<template>
  <div class="aded">
    <el-dialog
      @close="cancel"
      :title="title"
      :visible="showAded"
      :close-on-click-modal="false"
    >
      <el-form
        ref="myform"
        class="myform"
        label-width="100px"
        :model="form"
        :rules="rules"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见" prop="enVisible">
          <!-- <el-input v-model="form.enVisible"></el-input> -->
          <div class="switchBox">
            <el-switch
              style="display: block"
              v-model="form.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="可见"
              active-value="1"
              inactive-text="不可见"
              inactive-value="0"
            >
            </el-switch>
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  querySysPermissionApi,
  setSysPermissionApi,
  addSysPermissionApi,
} from "@/api/permission";
export default {
  name: "aded",
  props: {
    showAded: { required: true, type: Boolean },
  },
  data() {
    return {
      form: {
        id: "",
        name: "",
        code: "",
        description: "",
        enVisible: "1",
      },
      rules: {
        name: [{ required: true, message: "不合法", trigger: "blur" }],
        code: [{ required: true, message: "不合法", trigger: "blur" }],
        description: [{ required: true, message: "不合法", trigger: "blur" }],
        enVisible: [{ required: true, message: "不合法", trigger: "blur" }],
      },
      title: "编辑更新权限点",
    };
  },
  methods: {
    // 添加权限点
    async addSysPermission() {
      await addSysPermissionApi(this.form);
      console.log(this.form);
    },

    // 设置form。pid和type值
    setProperty(type, pid) {
      this.form.type = type;
      this.form.pid = pid;
      this.title = type === "1" ? "添加菜单" : "添加按钮权限点";
    },
    // 根据id修改权限点
    async setSysPermission() {
      await setSysPermissionApi(this.form);
    },
    // 根据ID获取查看权限点
    async querySysPermission(id) {
      this.form = await querySysPermissionApi(id);
      console.log(this.form);
    },
    // 保存修改
    save() {
      this.$refs.myform.validate(async (res) => {
        if (res) {
          // 是否是 修改编辑
          if (this.form.id) {
            await this.setSysPermission();
          } else {
            console.log("其他新增");
            await this.addSysPermission();
          }
          this.$message.success("修改成功");
          this.$parent.getSysPermission();
          this.$emit("update:showAded", false);
        }
      });
    },
    // 关闭弹窗
    cancel() {
      this.$emit("update:showAded", false);
      this.form = {
        id: "",
        name: "",
        code: "",
        description: "",
        enVisible: "",
      };
      this.$refs.myform.resetFields();
      this.title = "编辑更新权限点";
    },
  },
};
</script>

<style lang="scss" scoped>
.aded {
  .myform {
    .switchBox {
      height: 40px;
      display: flex;
      align-items: center;
    }
  }
}
</style>